﻿module nGo.Mvc.Tests

open System
open System.Data.Entity
open System.Data.Entity.Infrastructure
open System.Web.Http
open System.Web.Mvc
open System.Web.Optimization
open System.Web.Routing

open nGo
open nGo.Web.Mvc

[<Test.Case>]
let testRegisterAreas () =
    let testAreaRegistration = SingletonAction((fun () -> ()), false)
    Test.no "areas registered false" (testAreaRegistration.IsInvoked)

    Areas.registerAreas testAreaRegistration
        |> Test.yes "areas registered true"
    
    Areas.registerAreas testAreaRegistration
        |> Test.yes "re-registering areas still returns true"

[<Test.Case>]
let testSetDefaultDb () =
    Mock.create<IDbConnectionFactory> 
        |> Mock.init
        |> Databases.setDefaultDb
        |> Test.same "db same" Database.DefaultConnectionFactory

[<Test.Case>]
let testAddFilter () =
    let filter = HandleErrorAttribute()
    GlobalFilterCollection()
        |> (fun filters -> Test.eq "filter count before" filters.Count 0; filters)
        |> Filters.addFilter filter
        |> (fun filters -> 
            Test.eq "filter count after" filters.Count 1
            Test.yes "filter smae" (filters.Contains(filter)) )

[<Test.Case>]
let testIgnoreRoute () =
    let url = "blah/deedah"
    RouteCollection()
        |> (fun routes -> Test.eq "route count before" routes.Count 0; routes)
        |> Routes.ignoreRoute url
        |> (fun routes -> 
            Test.eq "route count after" routes.Count 1
            let route = routes.Item(0) :?> Route
            Test.eq "route url" url route.Url)

open Routes

[<Test.Case>]
let testMapHttpRoute () =
    let name, url, id = ("routename", "blah/deedah", RouteParameter.Optional)
    let defaults = { Routes.HttpRoute.id = id }
    RouteCollection()
        |> (fun routes -> Test.eq "route count before" routes.Count 0; routes)
        |> Routes.mapHttpRoute name url defaults
        |> (fun routes -> 
            Test.eq "route count after" routes.Count 1
            let route = routes.Item(0) :?> Route
            Test.eq "route url" url route.Url
            Test.yes "route default id key" (route.Defaults.ContainsKey("id"))
            Test.same "route default id" (route.Defaults.Item("id")) id)

[<Test.Case>]
let testMapControllerRoute () =
    let name, url, controller, action, id = ("routename", "blah/deedah", "testcontroller", "testaction", UrlParameter.Optional)
    let defaults = { ControllerRoute.controller = controller; action = action; id = id }
    RouteCollection()
        |> (fun routes -> Test.eq "route count before" routes.Count 0; routes)
        |> Routes.mapControllerRoute name url defaults
        |> (fun routes -> 
            Test.eq "route count after" routes.Count 1
            let route = routes.Item(0) :?> Route
            Test.eq "route url" url route.Url
            // defaults
            Test.yes "route default controller key" (route.Defaults.ContainsKey("controller"))
            Test.same "route default controller" (route.Defaults.Item("controller")) controller
            Test.yes "route default action key" (route.Defaults.ContainsKey("action"))
            Test.same "route default action" (route.Defaults.Item("action")) action
            Test.yes "route default id key" (route.Defaults.ContainsKey("id"))
            Test.same "route default id" (route.Defaults.Item("id")) id)

type TestBundles() =
    inherit Content.Bundles<TestBundles>()

    let mutable registered = false

    override x.RegisterTemplateBundles() = registered <- true; x

    override x.Count with get() = if registered then 1 else 0

[<Test.Case>]
let testBundles () =
    TestBundles()
        |> (fun bundles -> Test.eq "bundle count before" bundles.Count 0; bundles)
        |> Content.registerBundleTemplates
        |> (fun bundles -> Test.eq "bundle count after" bundles.Count 1;)
